﻿@using MvcContrib.UI.Grid
@using XRM.Security.WebUI.Consts
@model XRM.Security.WebUI.Models.SystemUser.ADUserDialogModel
@{
    Layout = null;
}

<style type="text/css">
    #table_adusers_id tbody tr
    {
        cursor: pointer;
    }
    #table_adusers_id .selected td {
        background-color: #428bca;
        color: white;
    }
</style>

@using (Html.BeginForm("ADUserChoice", "UserRegister", FormMethod.Post, new { id = "formADUsersGrid", @class = "form-horizontal form-horizontal-2" }))
{      
    @Html.AntiForgeryToken()
    @Html.HiddenFor(m => m.SelectedLogin)

    if (Model.ADUserList.Any())
    {
    <div style="height:300px;" class="table-scrollable">
        @{
        Html.Grid(Model.ADUserList).Columns(column =>
        {
            column.For(item => item.FullName).Named("Имя пользователя");
            column.For(item => item.AccountName).Named("Логин");
            column.For(item => item.Department).Named("Подразделение");
        }).Attributes(@class => "table table-striped table-bordered table-hover", id => "table_adusers_id").Render();
        }
    </div>
    <div id="divInfo" class="form-horizontal form-horizontal-2">
        <div class="form-group" style="display:block;margin-bottom:15px;">
            @Html.LabelFor(m => m.DepartmentId, new { @class = "control-label" })
            <div class="col-sm-6">
                @Html.DropDownListFor(m => m.DepartmentId, new SelectList(Model.DepartmentAllList, "Id", "Name"), new { @class = "form-control", style = "display:block;width:100%;" })
                @Html.ValidationMessageFor(m => m.DepartmentId)
            </div>
        </div>
        
        <div class="form-group"  style="display:block;">
            @Html.LabelFor(m => m.DepartmentAddressId, new { @class = "control-label" })
            <div class="col-sm-6">
                @Html.DropDownListFor(m => m.DepartmentAddressId, new SelectList(Model.DepartmentAddressAllList, "Id", "Address"), new { @class = "form-control", style = "display:block;width:100%;" })
                @Html.ValidationMessageFor(m => m.DepartmentAddressId)
            </div>
        </div>
    </div>
    }
    else
    {
        <text><p style="text-align:center;">Пользователей, удовлетворяющих заданным критериям поиска, не найдено</p></text>
    }
    <hr/>
    <div>
        <button type="button" class="btn btn-primary" id="btnChoiseADUser">Выбрать</button>
        <button type="button" class="btn btn-default" id="btnCancelADUser">Отмена</button>
    </div>
       
}    

@Scripts.Render("~/bundles/gridlibrary")
<script type="text/javascript" src="@(Url.Content("~/Scripts/AjaxRequest.js"))"></script>    
<script type="text/javascript">
    $(document).ready(function () {
        $('#btnCancelADUser').on('click', function () { CloseDialog(); });
        $('#btnChoiseADUser').on('click', function () { ChoiseUser(); });
        $('#btnChoiseADUser').prop('disabled', true);
        
        @if (Model.ADUserList.Any())
        {
            <text>
            $('#DepartmentId').change(function () {
                if ($(this).val() == '') {
                    $("#DepartmentAddressId").empty();
                    $('#DepartmentAddressId').prop('disabled', true);
                }
                else {
                    GetDepartmentAddress();
                }
            });
            IniDepartment();

            $('#table_adusers_id').dataTable({
                paging: false,
                bFilter: false,
                bSort: false,
                info: false
            });
            $('#table_adusers_id tbody').on('click', 'tr', function () {
                if ($(this).attr('id'))
                    if ($(this).attr('id') == "fakeRow")
                        return;

                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected');
                    $('#btnChoiseADUser').prop('disabled', true);
                }
                else {
                    $('#table_adusers_id').dataTable().$('tr.selected').removeClass('selected');
                    $(this).addClass('selected');
                    $('#btnChoiseADUser').prop('disabled', false);
                    var fr = $('#fakeRow');
                    if (fr.length == 0)
                        fr = GetFakeRow();
                    else
                        fr = fr.detach();
                    $(this).after(fr);
                    IniDepartment($(this).children().last().text().trim());
                }
            });

            $('#table_adusers_id tbody tr:first').trigger("click");
            </text>
        }
    });

    function IniDepartment(depName) {
        var currId = '';
        $('#DepartmentId option').each(function () {
            if ($(this).text() == depName) {
                currId = $(this).val();
                return;
            }
        });
        $('#DepartmentId').val(currId);
        $('#DepartmentId').change();
    }

    function GetFakeRow() {
        var td = $("<td colspan='3'></td>");
        td.append($("#divInfo"));
        var row = $("<tr id='fakeRow'></tr>");
        row.append(td);

        return row;
    }

    function ChoiseUser() {
        var trSelected = $('#table_adusers_id').dataTable().$('tr.selected');
        if (trSelected) {
            debugger;
            $('#SelectedLogin').val(trSelected.find("td").eq(1).html());
            $("#formADUsersGrid").submit();
        }
        else {
            alert('Нет выбранных пользователей!');
        }
    }
</script>
